package com.ruoyi.weixin.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.weixin.shop.domain.WxShop;
import com.ruoyi.weixin.shop.domain.vo.WxShopAddBo;
import com.ruoyi.weixin.shop.domain.vo.WxShopEditBo;
import com.ruoyi.weixin.shop.domain.vo.WxShopQueryBo;
import com.ruoyi.weixin.shop.domain.vo.WxShopVo;
import com.ruoyi.weixin.shop.mapper.WxShopMapper;
import com.ruoyi.weixin.shop.service.IWxShopService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 微信商城Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class WxShopServiceImpl extends ServiceImpl<WxShopMapper, WxShop> implements IWxShopService {

	@Override
	public WxShopVo queryById(Long id){
		return getVoById(id, WxShopVo.class);
	}

	@Override
	public TableDataInfo<WxShopVo> queryPageList(WxShopQueryBo bo) {
		PagePlus<WxShop, WxShopVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), WxShopVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<WxShopVo> queryList(WxShopQueryBo bo) {
		return listVo(buildQueryWrapper(bo), WxShopVo.class);
	}

	private LambdaQueryWrapper<WxShop> buildQueryWrapper(WxShopQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<WxShop> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getName()), WxShop::getName, bo.getName());
		lqw.eq(StrUtil.isNotBlank(bo.getLogo()), WxShop::getLogo, bo.getLogo());
		lqw.eq(StrUtil.isNotBlank(bo.getDomain()), WxShop::getDomain, bo.getDomain());
		lqw.eq(StrUtil.isNotBlank(bo.getCopyright()), WxShop::getCopyright, bo.getCopyright());
		lqw.eq(bo.getAboutUs() != null, WxShop::getAboutUs, bo.getAboutUs());
		lqw.eq(bo.getPrivacyProtocol() != null, WxShop::getPrivacyProtocol, bo.getPrivacyProtocol());
		lqw.eq(bo.getUserProtocol() != null, WxShop::getUserProtocol, bo.getUserProtocol());
		lqw.eq(StrUtil.isNotBlank(bo.getVersion()), WxShop::getVersion, bo.getVersion());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(WxShopAddBo bo) {
		WxShop add = BeanUtil.toBean(bo, WxShop.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(WxShopEditBo bo) {
		WxShop update = BeanUtil.toBean(bo, WxShop.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(WxShop entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
